home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / alib10.zip / COMPRES.ASM < prev    next >
Assembly Source File  |  1994-03-20  |  7KB  |  274 lines

  1. page    59,132
  2. comment 
  3.                              COMPRES & RESTOR
  4.                              ----------------
  5.  
  6.      Purpose:
  7.      --------
  8.  
  9.      COMPRES and RESTOR are sample program to show how huffman
  10.      compression works.  Single files can be compressed by the
  11.      COMPRES program, then decompressed by the RESTOR program.
  12.  
  13.      Using COMPRES & RESTOR
  14.      ----------------------
  15.  
  16.      To compress a file type "COMPRES file1 file2".  The first
  17.      file (file1) is the file to be compressed and the
  18.      second file is what to call the result.  RESTOR works
  19.      in the reverse.  "RESTOR file1 file2" will decompress
  20.      file1 and put at file2.
  21.  
  22.      Compression Overview
  23.      --------------------
  24.  
  25.      The compression/decompression engines are initiated with a
  26.      pointer to two routines.  One routine will feed data to the
  27.      engine and the other will store the output.  Next, one call
  28.      to the compression entry point will continue compressing
  29.      until the input (feed) routine runs out of data.
  30.      
  31.      Performance
  32.      -----------
  33.  
  34.      Compression speed and size is respectable, but not competative
  35.      with programs such as PKZIP and ARJ.  For most compression jobs
  36.      of less than a megabyte the speed will be acceptable.
  37. 
  38.      
  39.     extrn    shrink:far
  40.     extrn    expand:far
  41.     extrn    DWORD_TO_HEX_CRT:far
  42.     extrn    library_setup:far
  43.     extrn    library_terminate:far
  44.     extrn    lib_error_handler:far
  45.     extrn    parse_first:far
  46.     extrn    parse_next:far
  47.     extrn    file_size1:far
  48.     extrn    SHOW_CURSOR:far
  49.     extrn    key_read:far
  50.     extrn    lib_info:byte    
  51.     
  52. .xlist
  53.     include  mac.inc
  54.     include    common.inc
  55. .list
  56.   
  57. code    segment
  58.     assume    cs:code,ds:nothing
  59. ;▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
  60. ; FEED - fill buffer for shrink/expand
  61. ;  inputs: ds:dx = buffer ptr of size (disk_buf_size)
  62. ;             ax = requested amount
  63. ;  output:    ax = number of bytes placed in buffer, 0=end of input
  64. ;-------------------
  65. feed_handle    dw    0
  66. feed_count    dd    0
  67. ;-------------------
  68. feed    proc    far
  69.     apush    cx,bx
  70.     mov    cx,ax            ;amount of data to read
  71.     mov    bx,cs:feed_handle    ;file handle
  72.     mov    ah,3fh            ;read code
  73.     int    21h
  74.     add    word ptr cs:feed_count,ax
  75.     adc    word ptr cs:feed_count+2,0
  76.     apush    ax,dx
  77.     mov    ah,3
  78.     mov    bh,0
  79.     int    10h            ;read cursor posn -> dx
  80.     mov    dl,8
  81.     mov    ah,2
  82.     int    10h            ;set cursor posn
  83.     mov    ax,word ptr cs:feed_count    
  84.     mov    dx,word ptr cs:feed_count+2
  85.     call    DWORD_TO_HEX_CRT
  86.     apop    dx,ax
  87.     apop    bx,cx
  88.     retf
  89. feed    endp
  90.  
  91. ;▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
  92. ; STORE - save buffer data somewhere
  93. ;  inputs:  ds:dx = buffer ptr
  94. ;           ax = amount of data present in buffer
  95. ;  output:  none
  96. ;-------------------
  97. store_handle    dw    0
  98. store_count    dd    0
  99. ;-------------------
  100. store    proc    far
  101.     apush    ax,bx,cx
  102.     add    word ptr cs:store_count,ax
  103.     adc    word ptr cs:store_count+2,0
  104.     mov    cx,ax            ;set write length
  105.     mov    bx,cs:store_handle
  106.     mov    ah,40h
  107.     int    21h
  108.     push    dx
  109.     mov    ah,3
  110.     mov    bh,0
  111.     int    10h            ;read cursor posn -> dx
  112.     mov    dl,27
  113.     mov    ah,2
  114.     int    10h            ;set cursor posn
  115.     mov    ax,word ptr cs:store_count    
  116.     mov    dx,word ptr cs:store_count+2
  117.     call    DWORD_TO_HEX_CRT
  118.     pop    dx
  119.     apop    cx,bx,ax
  120.     retf
  121. store    endp
  122.  
  123.  
  124. ;▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
  125.  
  126.     assume    cs:code,ds:code
  127.  
  128.  
  129. ;██████████████████████████████████████████████████████████████████████████
  130.     
  131. infile_asciiz    db    40 dup (0)
  132. outfile_asciiz    db    40 dup (0)
  133. help_msg      db 0dh,0ah
  134.           db  'COMPRESS compresses a file to smaller size.',0dh,0Ah
  135.               db  'usage: ',0dh,0Ah
  136.               db  '   COMPRESS <file1> <file2> ',0dh,0ah
  137.               db 0dh,0ah
  138.               db  ' <file1> is compressed and stored at <file2>',0dh,0ah
  139.               db  'Press any key to continue.$'
  140. info_msg    db    'Reading            Writing$'              
  141. ;--------------------------
  142. start           proc    far
  143.         cld
  144.         mov    bp,ds        ;save PSP seg
  145.         mov    bx,cs
  146.         mov    ds,bx
  147.         mov    es,bx
  148.  
  149. ; next, release memory beyond the end of the program
  150. ; The  definition for ZSEG marks the
  151. ; end of the program's code, data and stack area.
  152.  
  153.     mov    ax,zseg
  154.  
  155.     mov    bx,bp
  156.     mov    es,bp
  157.     sub    bx,ax
  158.     neg    bx            ; size of program in paragraphs
  159.     mov    ah,4Ah            ; resize memory block
  160.     int    21h
  161.     push    ds
  162.     pop    es
  163. ;
  164. ; check if enough memory free to run program
  165. ;
  166.     mov    ax,bp            ;pass psp segment to setup
  167.     mov    bx,0            ;number of floating point variables
  168.     call    library_setup
  169.     cmp    ax,128
  170.     jae    got_enough_mem        ;jmp if 128k of memory avail
  171.     mov    al,7
  172.     mov    ah,fatal_return
  173.     call    lib_error_handler
  174.     jmp    exitx
  175.     
  176. got_enough_mem:
  177.     push    ds
  178.     pop    es
  179.     mov    di,offset infile_asciiz
  180.     call    parse_first
  181.     cmp    bh,7
  182.     jne    parse_err
  183.     mov    di,offset outfile_asciiz
  184.     call    parse_next
  185.     cmp    bh,7
  186.     jne    parse_err
  187. ;
  188. ; open the input file
  189. ;
  190.     mov    ax,3d00h
  191.     mov    dx,offset infile_asciiz
  192.     int    21h
  193.     jc    parse_err
  194.     mov    feed_handle,ax
  195.     mov    ax,3c00h
  196.     mov    cx,0                ;file attributes
  197.     mov    dx,offset outfile_asciiz
  198.     int    21h
  199.     mov    store_handle,ax
  200. ;
  201. ; setup to display
  202. ;
  203.     mov    dx,1800h
  204.     call    SHOW_CURSOR
  205.     mov    ah,9
  206.     mov    dx,offset info_msg
  207.     int    21h    
  208. ;
  209. ; setup for shrink/expand call
  210. ;
  211.     mov    si,offset feed
  212.     mov    di,offset store
  213.     push    cs
  214.     pop    es
  215. ;
  216. ; decode operation type
  217. ;
  218.     mov    bx,feed_handle
  219.     call    file_size1
  220.     call    shrink
  221.     jmp    exitx
  222.  
  223. parse_err:
  224.     mov    dx,offset help_msg
  225.     mov    ah,9
  226.     int    21h
  227.     call    key_read
  228.  
  229. ; normal program exit
  230.  
  231. exitx:    mov    bx,feed_handle
  232.     cmp    bx,0
  233.     je    exitx1
  234.     mov    ah,3eh
  235.     int    21h            ;close infile
  236. exitx1:    mov    bx,store_handle
  237.     cmp    bx,0
  238.     je    exitx2
  239.     mov    ah,3eh
  240.     int    21h
  241.         
  242. exitx2:    mov    ax,1            ;do not clear screen
  243.     call    library_terminate
  244.     mov    ax,4C00h
  245.     int    21h
  246.     
  247. start           endp
  248.   
  249. ;▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
  250.   
  251. code    ENDS
  252. ;--------------------------------------------------------- stack_seg_e  ---
  253. stack     segment para stack
  254.                 db      2048 dup (0)
  255. stack     ends
  256. ;
  257. ;-------------------------------------------------------------------------
  258. ;
  259. ; This segment definition is needed so linker will put the LIBSEG here
  260. ; before the ZSEG.  We want ZSEG to be last so memory allocation will
  261. ; work correctly.
  262. ;
  263. LIBSEG           segment byte public 'LIB'
  264. LIBSEG    ENDS
  265. ;-------------------------------------------------------------------------
  266. ; zseg must be at the end of the program for memory allocation from
  267. ; DOS.
  268. ;
  269.  
  270. zseg    segment para public 'ZZ'
  271. zseg    ends
  272.  
  273.                 end     start
  274.